package com.yulebaby.callsenter.ui.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.yulebaby.callsenter.R;

/**
 * Author: Zhangbo
 * Time: 2016年11月12日
 */
public class StateViewLayout extends FrameLayout {


    private View loadingView;
    private View errorView;
    private View successView;

    public StateViewLayout(Context context, AttributeSet attrs) {

        this(context, attrs, 0);
    }

    public StateViewLayout(Context context) {
        this(context, null);
    }

    public StateViewLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        loadingView = View.inflate(getContext(), R.layout.page_loading, null);
        addView(loadingView);

        errorView = View.inflate(getContext(), R.layout.page_error, null);
        Button btn_reload = (Button) errorView.findViewById(R.id.btn_reload);
        btn_reload.setOnClickListener(v -> {
            if (listener != null) {
                listener.onReloadBtnClick();
            }
        });
        addView(errorView);
        hideAllView();
    }


    public void setSuccessView(View successView) {
        this.successView = successView;
        this.successView.setVisibility(View.INVISIBLE);
        addView(this.successView);
    }

    public void showLoadingView() {
        hideAllView();
        loadingView.setVisibility(View.VISIBLE);
    }

    private void hideAllView() {
        loadingView.setVisibility(View.INVISIBLE);
        errorView.setVisibility(View.INVISIBLE);
        if (successView != null) {
            successView.setVisibility(View.INVISIBLE);
        }
    }

    public void showErrorView() {
        hideAllView();
        errorView.setVisibility(View.VISIBLE);
    }


    public void showSuccessView() {
        hideAllView();
        if (successView != null) {
            successView.setVisibility(View.VISIBLE);
        }
    }

    OnReloadBtnClickListener listener;

    public void setOnReloadClickListener(OnReloadBtnClickListener listener) {
        this.listener = listener;
    }

    public interface OnReloadBtnClickListener {
        void onReloadBtnClick();
    }
}
